package protocol

import (
	"encoding/binary"
	"errors"
	"gitee.com/crack007/goose"
)

func UnpackData(data []byte) (IProtocol, error) {
	code := binary.BigEndian.Uint32(data[:4])
	goose.GetLogger().Trace("request code=%d", code)
	iProtocol := GetProtocol(code)
	if iProtocol == nil {
		return nil, errors.New("code error")
	}
	iProtocol.UnMarshall(data[4:])
	return iProtocol, nil
}

func PackData(iProtocol IProtocol) ([]byte, error) {
	var body = make([]byte, 4)
	binary.BigEndian.PutUint32(body[:4], iProtocol.Code())
	resp := iProtocol.Marshall()
	body = append(body, resp...)
	return body, nil
}
